<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>缺货登记</title>
    <th:block th:include="~{common/common::commonHead}"/>
</head>
<style>
    #goods{
        float:right;
        position: relative;
    }
</style>
<body data-width="850" data-height="500">
<form id="defaultForm" class="form-horizontal" role="form" th:action="@{/goodsReturn/addGoodsReturn}">
    <!--<input type="hidden" id="goodsId" name="goodsId">-->
    <div class="col-sm-6 col-xs-12">
        <div id="localImg" style="padding: 10px;border: 1px dashed #ddd;">
            <img id="preview" th:src="@{/img/type/defaultGoodsImg.png}" width="280" height="280" style="display: block; margin: auto">
        </div>
        <div class="iconic-input right">
            <input type="file" name="file" id="doc" style="width:78px;height: 32px; opacity: 0; position: relative; margin-top: -30px; margin-left: 160px;" onchange="setImagePreview();">
        </div>
    </div>
    <div class="col-sm-6 col-xs-12" id="goods">
        <input type="hidden" id="goodsImg" name="goodsImg">
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-md-2 col-sm-3 col-xs-4 control-label"> 犯人姓名 </label>
                <div class="col-md-6 col-sm-8 col-xs-8">
                    <div class="iconic-input right">
                        <select id="prisonerId" name="prisonerId" onchange="text();" class="form-control" required>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-md-2 col-sm-3 col-xs-4 control-label"> 商品名称 </label>
                <div class="col-md-6 col-sm-8 col-xs-8">
                    <div class="iconic-input right">
                        <select id="goodsId" name="goodsId" onchange="text1();" class="form-control" required>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-md-2 col-sm-3 col-xs-4 control-label"> 缺货数量 </label>
                <div class="col-md-6 col-sm-8 col-xs-8">
                    <div class="iconic-input right">
                        <input id="goodsCount" name="goodsCount" type="number" class="form-control spinner"  data-valid-decimal="true" min="1" placeholder="请输入缺货数量" required>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

</body>
</html>
<script type="text/javascript">
    //下面用于图片上传预览功能
    function setImagePreview() {
        var docObj=document.getElementById("doc");
        var formData = new FormData();
        formData.append("files", docObj.files[0]);
        $.ajax({
            url: "file/upload",
            type: "POST",
            data: formData,
            processData : false,// 告诉jQuery不要去处理发送的数据
            contentType : false,// 告诉jQuery不要去设置Content-Type请求头
            beforeSend:function(){
                console.log("正在进行，请稍候");
            },
            success : function(data) {
                if (data.code !== '0') {
                    $.error(data.msg);
                } else {
                    var url = data.data[0].url;
                    console.log(url);
                    $("#goodsImg").val(url);
                    $("#preview").attr("src", url);
                }
            },
            error : function() {
                $.error("上传失败");
            }
        });
    }
    $(function(){
        $("#prisonerId").append("<option value=''>请选择犯人</option>");
        $("#goodsId").append("<option value=''>请选择商品</option>");
        var invoiceId = $.getUrlParams();
            $.doAjax("invoice/getPrisonerInfoByInvoiceId",invoiceId,function(data){
                for(var i=0;i<data.length;i++) {
                    $("#prisonerId").append("<option value='" + data[i].id + "'>" + data[i].name + "</option>")
                }
            });
    });
    function text(){
        var arr={};
        var prisonerId = $("#prisonerId").val();
        var invoiceId = $.getUrlParams().idStr;
       arr.prisonerId=prisonerId;
       arr.invoiceId=invoiceId;
        var json = JSON.stringify(arr);
        $.doAjax("invoice/getGoodsHistoryDetail",{goodsReturn:json},function(data){
            $("#goodsId").empty().append($("<option value=''>请选择商品</option>"));
            for(var i=0;i<data.length;i++){
                $("#goodsId").append("<option value='" + data[i].id + "'>" + data[i].goodsName + "</option>");
            }
        })

    }
    function text1(){
        var id = $("#goodsId").val();
        $.doAjax("goodsHistoryDetail/getGoodsHistoryDetail",{id:id},function(goods){
            $("#preview").attr("src",goods.goodsImg);
        })
    }
</script>

